package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShopCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishItemVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShopCartMapper shopCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetMealDishMapper setmealDishMapper;


    /**
     * 添加商品到购物车
     * @param shoppingCartDTO
     */
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        log.info("添加商品到购物车:{}" , shoppingCartDTO);
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        // 获取用户ID
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        //先查询购物车表中有没有该数据，有的话执行update将数量加1
        List<ShoppingCart> list = shopCartMapper.list(shoppingCart);
        if(list != null && list.size()>0) {
            // 获取查询到的第一条数据
            ShoppingCart shoppingCart1 = list.get(0);
            shoppingCart1.setNumber(shoppingCart1.getNumber() + 1);
            shopCartMapper.updateNunberById(shoppingCart1);
        }
        //没有的话执行insert插入一条数据
        else {
            //本次插入是菜品还是套餐
            Long dishId = shoppingCartDTO.getDishId();
            Long setMealId = shoppingCartDTO.getSetmealId();
            // 公共属性
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            if(dishId != null) {
                shoppingCart.setDishId(dishId);
                Dish dish = dishMapper.selectDetailById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            } else {
                shoppingCart.setSetmealId(setMealId);
                Setmeal setmeal = setmealDishMapper.selectByPrimaryKey(setMealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            // 插入购物车表
            shopCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 获取购物车列表
     * @return
     */
    public List<ShoppingCart> getShoppingCartList() {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = ShoppingCart.builder().userId(userId).build();
        List<ShoppingCart> list = shopCartMapper.list(shoppingCart);
        return list;
    }

    /**
     * 清空购物车
     */
    public void clean() {
        // 获取用户ID，清空购物车表
        Long userId = BaseContext.getCurrentId();
        shopCartMapper.deleteByUserId(userId);
    }

    /**
     * 删除购物车商品
     * @param shoppingCartDTO
     */
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        // 获取用户ID
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        // 查询该条数据
        List<ShoppingCart> list = shopCartMapper.list(shoppingCart);
        // 将该数据的数量减一，如果数量为0，则删除该条数据
        ShoppingCart shoppingCart1 = list.get(0);
        shoppingCart1.setNumber(shoppingCart1.getNumber() - 1);
        if(shoppingCart1.getNumber() == 0) {
            shopCartMapper.delete(shoppingCart1);
        } else {
            shopCartMapper.updateNunberById(shoppingCart1);
        }

    }
}
